package com.mystyle.le.liuxiang.utils;

import android.util.Log;

import com.mystyle.le.liuxiang.beans.HeadList;
import com.mystyle.le.liuxiang.beans.HeadPager;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by le on 2016/10/26.
 */
public class ParseData {
    /*
    * 解析娱乐头条的头布局的滑动数据
    * */
    public static List<HeadPager> parseHeadPager(String path){
        List<HeadPager> list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(path);
            if (jsonObject!=null){
                JSONObject data = jsonObject.getJSONObject("data");
                if (data!=null){
                    JSONArray hot_pic = data.getJSONArray("hot_pic");
                    if (hot_pic!=null&&hot_pic.length()>0){
                        for (int i =0;i<hot_pic.length();i++){
                            JSONObject jsonObject1 = hot_pic.getJSONObject(i);
                            JSONObject item = jsonObject1.getJSONObject("item");
                            String title = item.getString("title");
                            JSONArray imgs = item.getJSONArray("imgs");
                            if (imgs!=null&&imgs.length()>0){
                                for (int j =0;j<imgs.length();j++){
                                    JSONObject jsonObject2 = imgs.getJSONObject(j);
                                    String img = jsonObject2.getString("img");
                                    list.add(new HeadPager(title,img));
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("MeiYingXiang", "parseHeadPager: "+list.size());
        return list;
    }
    /*
    * 解析娱乐头条的listview的数据
    * */
    public static List<HeadList> parseList(String path){
        List<HeadList> lists = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(path);
            if (jsonObject!=null){
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray headlines = data.getJSONArray("headlines");
                    for (int i =0;i<headlines.length();i++){
                        JSONObject jsonObject1 = headlines.getJSONObject(i);
                        JSONObject item = jsonObject1.getJSONObject("item");
                            String title = item.getString("title");
                            String user_name = item.getString("user_name");
                            String user_avatar = item.getString("id");
                            JSONArray imgs = item.getJSONArray("imgs");
                            String img = null;
                            for (int j =0;j<imgs.length();j++){
                                JSONObject jsonObject2 = imgs.getJSONObject(j);
                                img = jsonObject2.getString("img");
                            }
                            lists.add(new HeadList(title,user_name,user_avatar,img));
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return lists;
    }
}
